﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.students
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        Poll p1 = new Poll();
        polloption p2 = new polloption();
        protected void Page_Load(object sender, EventArgs e)
        {
            int[] c = new int[4];
            DataTable dt = p1.GetRecords(-99, "", true, false, " and ");
            Label1.Text = dt.Rows[0]["question"].ToString();
            DataTable dt1 = p2.GetRecords(-99, Convert.ToInt64(dt.Rows[0]["poll_id"].ToString()), "", -99, false, " and ");
            if (dt1.Rows.Count > 0)
                for (int i = 0; i < dt1.Rows.Count; i++)
                    RadioButtonList1.Items.Add(dt1.Rows[i]["answer"].ToString());
            GridView1.DataSource = dt1;
            GridView1.DataBind();

        }

        protected void btnvote_Click(object sender, EventArgs e)
        {
            long[] count = new long[4];
            DataTable dt = p1.GetRecords(-99, "", true, false, " and ");
            DataTable dt1 = p2.GetRecords(-99, Convert.ToInt64(dt.Rows[0]["poll_id"].ToString()), "", -99, false, " and ");
            if (dt1.Rows.Count > 0)
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    count[j] = Convert.ToInt64(dt1.Rows[j]["vote"].ToString());
                    if (RadioButtonList1.SelectedValue == dt1.Rows[j]["answer"].ToString())
                    {
                        count[j] = count[j] + 1;
                        p2.option_id = Convert.ToInt64(dt1.Rows[j]["option_id"].ToString());
                        p2.poll_id = Convert.ToInt64(dt1.Rows[j]["poll_id"].ToString());
                        p2.answer = dt1.Rows[j]["answer"].ToString();
                        p2.vote = count[j];
                        p2.isdeleted = false;
                        p2.SaveRecords();
                    }

                }
            Response.Redirect("Spolling.aspx");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}
